//
//  NetworkTools.m
//  TestAFN
//
//  Created by China Dragon on 2023/8/27.
//

#import "NetworkTools.h"
// 此方法仅在 OC 里有效
/// 网络工具协议
@protocol NetworkToolsProxy <NSObject>

/// 网络请求方法 -- 此方法是负责 AFHTTPSessionManager 类里的
@optional // 方法可以不实现
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
                                       URLString:(NSString *)URLString
                                      parameters:(nullable id)parameters
                                         headers:(nullable NSDictionary <NSString *, NSString *> *)headers
                                  uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
                                downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
                                         success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                                         failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;

@end

/// 遵守网络协议 --  为了能够欺骗xcode 给一个 智能提示，以及编译通过
@interface NetworkTools() <NetworkToolsProxy>

@end


@implementation NetworkTools

+ (instancetype)sharedTools{
    static NetworkTools *tools;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // https://e.weather.com.cn/mweather/101020300.shtml
        // http://www.weather.com.cn/weather1d/101020100.shtml
        // http://t.weather.itboy.net/api/weather/city/101030100
        
        /*
         
         https://httpbin.org/  主要给做网络框架测试请求用的
         
         httpbin.org的使用
         https://blog.csdn.net/Hubz131/article/details/89157089
         
         http测试工具：httpbin
         https://www.linfengnet.com/web/256.html
         */
    
        // 查看 AFURLSessionManager 源码即可 查看 baseUrl 示例
        // 注意末尾 需要包含 '/'
//        NSURL *baseUrl = [NSURL URLWithString:@"http://www.weather.com.cn/"];
//        NSURL *baseUrl = [NSURL URLWithString:@"http://e.weather.com.cn/"];
        
//        NSURL *baseUrl = [NSURL URLWithString:@"http://t.weather.itboy.net/"];
        NSURL *baseUrl = [NSURL URLWithString:@"https://httpbin.org/"];
        tools = [[self alloc] initWithBaseURL:baseUrl];
        
        /*
         设置反序列化格式
         查看 AFJSONResponseSerializer 源码 找到 AFJSONResponseSerializer init
         默认格式 [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
         */
        tools.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    });
    
    return tools;
}

-(void)request:(DemoRequestMethod)method URLString:(NSString *)URLString parameters:(id)parameters finished:(void (^)(id _Nullable, NSError * _Nullable))finished{
    
    NSString *methodName = (method == GET) ? @"GET" : @"POST";
    
    /*
     dataTaskWithHTTPMethod 本类没有实现此方法，父类实现了
     在调用方法的时候，如果本类没有提供，直接调用父类的方法，AFN 内部类已经实现了
     */
    [[self dataTaskWithHTTPMethod:methodName URLString:URLString parameters:parameters headers:nil uploadProgress:nil downloadProgress:nil success:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
            finished(responseObject,nil);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
            NSLog(@" failure -> %@",error);
//            finished(nil,error);
        }] resume];
    
    
//    if(method == GET){
//
//        [self GET:URLString parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//    //        NSLog(@" success -> %@",responseObject);
//            finished(responseObject,nil);
//
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//    //        NSLog(@" failure -> %@",error);
//            finished(nil,error);
//        }];
//    }else{
//        [self POST:URLString parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//    //        NSLog(@" success -> %@",responseObject);
//            finished(responseObject,nil);
//
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            NSLog(@" failure -> %@",error);
//            finished(nil,error);
//        }];
//    }

    
}

@end
